package com.example.demo.threadDemo.thtradPools;

import java.util.concurrent.*;

/**
 * @Description ThtradPoolTest1
 * @Author songchuanlong
 * @Date 2025-05-11
 */


/**
 * 线程池的创建
 * new ThreadPoolExecutor.AbortPolicy()  直接抛异常，无法解决这个任务
 */
public class ThtradPoolTest2 {
    public static void main(String[] args) throws Exception{
        //1、通过ThreadPoolExecutor创建一个线程池对象
//        ExecutorService pool = new ThreadPoolExecutor(3,
//                5,
//                8,
//                TimeUnit.SECONDS,
//                new ArrayBlockingQueue<>(4),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());

        //1-2通过Executors创建一个线程池对象。
//        ExecutorService pool = Executors.newFixedThreadPool(3);
        ExecutorService pool = Executors.newSingleThreadExecutor();
        //使用线程池处理callable任务
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}
